import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcom from '../components/welcom.vue'
import user from '../components/user.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
import cate from '../components/goods_cats/Cate.vue'
import params from '../components/goods_cats/params.vue'
import list from '../components/goods_cats/list.vue'
import addgoods from '../components/goods_cats/addgoods.vue'
import order from '../components/order/order.vue'
import report from '../components/report/report.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
      path: '/',
      redirect: '/login'
    }, {
      path: '/login',
      component: login
    },
    {
      path: '/home',
      component: home,
      redirect: '/home/welcom',
      children: [{
          path: '/home/welcom',
          component: welcom
        }, {
          path: '/users',
          component: user
        }, {
          path: '/rights',
          component: rights
        },
        {
          path: '/roles',
          component: roles
        },
        {
          path: '/categories',
          component: cate
        },
        {
          path: '/params',
          component: params
        },
        {
          path: '/goods',
          component: list
        }, {
          path: '/goods/add',
          component: addgoods
        },
        {
          path: '/orders',
          component: order
        },
        {
          path: '/reports',
          component: report
        }
      ]
    }
  ]
})
// 登陆拦截 路由守卫
// 访问除了login之外的页面是 要进行登录判断
router.beforeEach((to, from, next) => {
  // to代表访问的路禁
  // from代表从哪来
  // next函数 放行
  if (to.path === '/login') {
    return next()
  }
  // 其他页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }
  // 登录了已经
  next()
})
export default router
